package com.lbs.jiepiaisi.utils;

import android.Manifest;
import android.app.Activity;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.AppOpsManagerCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import java.lang.reflect.Method;

/**
 * @author ydm
 * @date 2018/5/21
 * @desc
 */

public class LocationPerUtil {

    /**
     * 手机是否开启位置服务，如果没有开启那么所有app将不能使用定位功能
     */
    public static boolean isLocServiceEnable(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        Log.e("TAG==","GPS-LocationPerUtil=>"+gps);
        Log.e("TAG==","netWork-LocationPerUtil=>"+network);
        if (gps || network) {
            return true;
        }
        return false;
    }
  private static boolean checkPermissionGranted(Context context) {
      if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
              ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
          ActivityCompat.requestPermissions((Activity) context,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION},0);
          return false;
      }
      return true;
  }
    /**
     * 检查定位服务、权限
     */
    public static boolean checkLocationPermission(Context context) {
        if (!isLocServiceEnable(context)) {//检测是否开启定位服务
           return false;
        }
        return checkPermissionGranted(context);
    }
}
